package com.example.servlet;

import com.example.dao.NovelDao;
import com.example.domain.Novel;
import com.example.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/BookShelfAddServlet")
public class BookShelfAddServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        Novel novel= (Novel)session.getAttribute("novel");
        User user= (User)session.getAttribute("user");
        NovelDao novelDao=new NovelDao();
        int affectedRow = novelDao.addbookshelf(user.getUser_id(),novel.getNovel_id());
        if (affectedRow>0){
            request.setAttribute("msg","添加成功");
            request.getRequestDispatcher("novelDetail.jsp").forward(request,response);
            return;
        }else{
            if(novelDao.isFavorite(user.getUser_id(),novel.getNovel_id())){
                request.setAttribute("msg","小说已经收藏！");
            }else{
                request.setAttribute("msg","添加失败");
            }
            request.getRequestDispatcher("novelDetail.jsp").forward(request,response);
            return;
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
